<template>
  <div class="set">
    <div class="set_ct">
      <Form ref="formValidate"
            :model="formData"
            :rules="ruleValidate"
            :label-width="100"
            class="set_ct_form">
        <FormItem label="时间校验设置:"
                  prop="time">
          <TimePicker format="HH:mm"
                      :editable="false"
                      v-model="formData.time"
                      style="width:280px;"></TimePicker>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  class="btn"
                  @click="handleSubmit">确认</Button>
          <Button class="btn"
                  style="margin-left:20px;"
                  @click="cancel">取消</Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>
<script>
import { getDicQuery, setDicSave } from '@/api/setting'
import { routeEqual } from '@/libs/util'
import './index.less'
export default {
  data () {
    return {
      title: '刷卡器与服务器时间校验设置',
      formData: {
        time: ''
      },
      ruleValidate: {
        time: [{ required: true, type: 'string', message: '刷卡器与服务器时间不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    },
    handleSubmit () {
      this.$refs.formValidate.validate((val) => {
        if (val) {
          setDicSave({
            'serverCheckInterval': this.formData.time,
            'merchantId': this.$store.state.user.merchantId,
            'msType': '0'
          }).then(res => {
            if (res.data.status === 200) {
              this.$Message.success('修改成功')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    cancel () {
      this.closeModal()
    },
    submitForm () {
      getDicQuery({ 'merchantId': this.$store.state.user.merchantId, 'msType': '0', 'dictKeys': 'serverCheckInterval' }).then((res) => {
        if (res.data.status === 200) {
          this.formData.time = res.data.data[0].serverCheckInterval
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    }
  },
  mounted () {
    this.submitForm()
  }
}
</script>
